﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsUserInboxTableAdapters;
using ConstructionBidOnline.Common;


namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// UserInbox Business Object
    /// </summary>

    public class UserInbox : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected UserInbox() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the UserInbox.
        /// </summary>
        public int MessageID
        {
            get { return _messageID; }
            set
            {
                if (_messageID != value)
                {
                    if (OnPropertyChanging("MessageID", _messageID, value))
                    {
                        int oldValue = _messageID;
                        _messageID = value;

                        OnPropertyChanged("MessageID", oldValue, value);
                    }
                }
            }
        }  private int _messageID = -1;



        /// <summary>
        /// PKID of the SupportTickets.
        /// </summary>
        public string TicketID
        {
            get { return _TicketID; }
            set
            {
                if (_TicketID != value)
                {
                    if (OnPropertyChanging("TicketID", _TicketID, value))
                    {
                        string oldValue = _TicketID;
                        _TicketID = value;

                        OnPropertyChanged("TicketID", oldValue, value);
                    }
                }
            }
        }  private string _TicketID = null;


        /// <summary>
        /// MessageFrom.
        /// </summary>
        public string MessageFrom
        {
            get { return _messagefrom; }
            set
            {
                if (_messagefrom != value)
                {
                    if (OnPropertyChanging("MessageFrom", _messagefrom, value))
                    {
                        string oldValue = _messagefrom;
                        _messagefrom = value;
                        OnPropertyChanged("MessageFrom", oldValue, value);
                    }
                }
            }
        }  private string _messagefrom = null;



        /// <summary>
        /// MessageTo.
        /// </summary>
        public int MessageTo
        {
            get { return _messageto; }
            set
            {
                if (_messageto != value)
                {
                    if (OnPropertyChanging("MessageTo", _messageto, value))
                    {
                        int oldValue = _messageto;
                        _messageto = value;
                        OnPropertyChanged("MessageTo", oldValue, value);
                    }
                }
            }
        }  private int _messageto = -1;



        /// <summary>
        /// MessageTitle.
        /// </summary>
        public string MessageTitle
        {
            get { return _messagetitle; }
            set
            {
                if (_messagetitle != value)
                {
                    if (OnPropertyChanging("MessageTitle", _messagetitle, value))
                    {
                        string oldValue = _messagetitle;
                        _messagetitle = value;
                        OnPropertyChanged("MessageTitle", oldValue, value);
                    }
                }
            }
        }  private string _messagetitle = null;


        /// <summary>
        /// CompleteMessage.
        /// </summary>
        public string CompleteMessage
        {
            get { return _completemessage; }
            set
            {
                if (_completemessage != value)
                {
                    if (OnPropertyChanging("CompleteMessage", _completemessage, value))
                    {
                        string oldValue = _completemessage;
                        _completemessage = value;
                        OnPropertyChanged("CompleteMessage", oldValue, value);
                    }
                }
            }
        }  private string _completemessage = null;


        
        /// <summary>
        /// AttachedFile.
        /// </summary>
        public string AttachedFile
        {
            get { return _attachedfile; }
            set
            {
                if (_attachedfile != value)
                {
                    if (OnPropertyChanging("AttachedFile", _attachedfile, value))
                    {
                        string oldValue = _attachedfile;
                        _attachedfile = value;
                        OnPropertyChanged("AttachedFile", oldValue, value);
                    }
                }
            }
        }  private string _attachedfile = null;
        


        /// <summary>
        /// Date/time at which the UserInbox was created.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static UserInbox CreateNew()
        {
            UserInbox obj = new UserInbox();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.AddedDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static UserInbox CreateNew(UserInboxCollection collection)
        {
            UserInbox obj = CreateNew();
            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (UserInbox userinbox in collection)
            {
                if (userinbox.MessageID < minVal)
                    minVal = userinbox.MessageID;
            }
            obj.MessageID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetCompleteUserInbox

        /// <summary>
        /// Returns a collection of UserInbox.
        /// </summary>
        public static UserInboxCollection GetCompleteUserInbox()
        {
            UserInboxTableAdapter adapter = new UserInboxTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsUserInbox.UserInboxDataTable tbl = adapter.GetData(null);

            UserInboxCollection list = new UserInboxCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsUserInbox.UserInboxRow row in tbl.Rows)
                {
                    UserInbox UserInbox = new UserInbox();
                    RelationalToObject(UserInbox, row);
                    list.Add(UserInbox);
                }
            }

            return (list);
        }

        #endregion

        #region GetUserInboxByMessageID

        /// <summary>
        /// Returns a collection of UserInbox.
        /// </summary>
        public static UserInbox GetUserInboxByMessageID(int messageid)
        {
            UserInboxTableAdapter adapter = new UserInboxTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsUserInbox.UserInboxDataTable tbl = adapter.GetData(messageid);

            if (tbl.Rows.Count == 1)
            {
                UserInbox userinbox = new UserInbox();
                RelationalToObject(userinbox, tbl.Rows[0]);
                return (userinbox);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'userinbox' returned more than one User record.");

            return (null);
        }

        #endregion

        #region GetCompleteUserInboxByMessageTo

        /// <summary>
        /// Returns a collection of UserInbox.
        /// </summary>
        public static UserInboxCollection GetCompleteUserInboxByMessageTo(int messageto)
        {
            UserInboxTableAdapter adapter = new UserInboxTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsUserInbox.UserInboxDataTable tbl = adapter.GetDataByMessageTo(messageto);

            UserInboxCollection list = new UserInboxCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsUserInbox.UserInboxRow row in tbl.Rows)
                {
                    UserInbox UserInbox = new UserInbox();
                    RelationalToObject(UserInbox, row);
                    list.Add(UserInbox);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified UserInbox.
        /// </summary>
        public static void Update(UserInbox obj)
        {
            bool updateUserInbox = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateUserInbox = (obj.ObjState != ObjectState.Unchanged);

                if (!updateUserInbox)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsUserInbox.UserInboxDataTable tbl = null;
            if (updateUserInbox)
            {
                UserInboxTableAdapter adapter = new UserInboxTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsUserInbox.UserInboxDataTable();
                dsUserInbox.UserInboxRow row = tbl.NewUserInboxRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Delete

        /// <summary>
        /// Delete UserInbox from the table with the specified messageid
        /// </summary>
        public static UserInbox Delete(int msgid)
        {
            UserInboxTableAdapter adapter = new UserInboxTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            adapter.Delete(msgid);

            return (null);
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (MessageID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("MessageID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((MessageTitle == null) || (MessageTitle.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("MessageTitle", "MessageTitle is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("UserInbox: TicketID={0}, MessageTitle={1}, {2}", TicketID, MessageTitle));
        }

        #endregion

        #endregion        

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class UserInboxCollection : List<UserInbox>
    {
        #region Types

        public class UserComparer : IComparer<UserInbox>
        {
            public int Compare(UserInbox pm1, UserInbox pm2)
            {
                return (String.Compare(pm1.MessageTitle, pm2.MessageTitle));
            }
        }

        #endregion

        #region Public Methods

        #region FindByMessageID

        public UserInbox FindByMessageID(int msgid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].MessageID == msgid)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindByTicketID

        public UserInbox FindByJobID(string ticketid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].TicketID == ticketid)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
